package com.hanhai.zrb.mobile.controller;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanhai.common.annotation.LoginNeedless;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.PhotoService;
import com.hanhai.zrb.api.client.facade.FundFacade;
import com.hanhai.zrb.api.util.DateUtil;
import com.hanhai.zrb.mobile.result.BaseApiResult;
import com.hanhai.zrb.model.fund.vo.AppFundDetailVo;
import com.hanhai.zrb.model.fund.vo.AppFundVo;
import com.hanhai.zrb.model.photo.Photo;
import com.hanhai.zrb.model.project.Project;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;


@Controller
@RequestMapping("/fund")
public class FundlApiController extends BaseController{
	
	@Resource
	private FundFacade fundFacade;
	
	@Resource
	private PhotoService photoService;
	
	@LoginNeedless
	@ResponseBody
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "查询资金列表",  response = BaseApiResult.class, notes = "查询资金列表")
	@RequestMapping(value="/list")
	public BaseApiResult list(HttpServletRequest request,HttpServletResponse response, Long pushTime, Integer limit){
	    BaseApiResult result = new BaseApiResult();
	    Date date = (null==pushTime?null:DateUtil.getStringToDate(DateUtil.FULL_ST_FORMAT,DateUtil.getDate(pushTime)));
        List<AppFundVo> list = fundFacade.listAppFunds(date,limit,false);
        result.setData(list);
		result.setRes(1);
		logger.info("user query fund list successed");
		return result;
	}
	
	
	@LoginNeedless
	@ResponseBody
	@ApiResponses(value = { @ApiResponse(code = 405, message = "Invalid input") })
	@ApiOperation(value = "查询资金列表",  response = BaseApiResult.class, notes = "查询资金列表")
	@RequestMapping(value="/searchList")
	public BaseApiResult searchList(HttpServletRequest request,HttpServletResponse response, Long pushTime,String mode, String citys,String industrys, Integer limit){
	    BaseApiResult result = new BaseApiResult();
		List<String> destinationList= new ArrayList<String>();   
		List<String> industrysList= new ArrayList<String>();
		if(StringUtils.isNoneBlank(citys)){
			String[] cityStrs = citys.split(",");
			for (int i = 0; i < cityStrs.length; i++) {
				destinationList.add(cityStrs[i]);
			}
		}
		
		if(StringUtils.isNoneBlank(industrys)){
			String[] industryStr = industrys.split(",");
			for (int i = 0; i < industryStr.length; i++) {
				if(StringUtils.isNumeric(industryStr[i]) && !"97".equals(industryStr[i])){  //97是查询全部行业，把industry制空
					industrysList.add(industryStr[i]);
				}else{
					industrysList =null;
					break ;
				}
			}
		}		
		
	    
	    Date date = (null==pushTime?null:DateUtil.getStringToDate(DateUtil.FULL_ST_FORMAT,DateUtil.getDate(pushTime)));
        List<AppFundVo> list = fundFacade.searchAppFunds(date,destinationList,industrysList,mode,limit,false);
        result.setData(list);
		result.setRes(1);
		logger.info("user query fund list successed");
		return result;
	}
	
	
	@LoginNeedless
	@RequestMapping(value="/detail")
	public String detail(HttpServletResponse response,Long id,Model model){
		AppFundDetailVo detail = fundFacade.getAppFundDetail(id);
		List<Photo> photoListByFundId = photoService.getPhotoListByFundId(id);
		if( !photoListByFundId.isEmpty()){
			model.addAttribute("photoCount", photoListByFundId.size());
		}
		model.addAttribute("detail", detail);
		logger.info("user query fund detail successed");
		return "fund/fundsDetail";
	}
	
	@LoginNeedless
	@RequestMapping("getPhotoList")
	public void getPhotoList(HttpServletResponse response, Model model,@RequestParam Long id) {
		List<Photo> photoList = photoService.getPhotoListByFundId(id);
		super.returnObjectJsonString(response, photoList);
	}
	
	
	


	@RequestMapping(value="getUserFundList")
	@ResponseBody
	public BaseApiResult getUserFunds(Long userId,Date lastPushTime,Integer limit) {
		List<AppFundVo> list = fundFacade.listAppFunds(userId, lastPushTime, limit, false);
		BaseApiResult ret = new BaseApiResult();
		ret.setData(list);
		
		return ret;
	}
	
}
